package cn.jd.org.service.impl;

import cn.jd.basic.query.BaseQuery;
import cn.jd.basic.service.impl.BasicServiceImpl;

import cn.jd.basic.util.MD5Utils;
import cn.jd.basic.util.PageList;
import cn.jd.basic.util.StrUtils;
import cn.jd.org.domain.Employee;
import cn.jd.org.domain.EmployeeRole;
import cn.jd.org.mapper.EmployeeMapper;

import cn.jd.org.mapper.EmployeeRoleMapper;
import cn.jd.org.service.EmployeeService;
import cn.jd.system.domain.Role;
import cn.jd.system.mapper.RoleMapper;
import cn.jd.user.domain.Logininfo;
import cn.jd.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BasicServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;

    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        PageList<Employee> employeePageList = super.queryByPage(query);
        List<Employee> employeeList = employeePageList.getRows();


        // 返回所有的角色集合
        List<EmployeeRole> roleList = employeeRoleMapper.findAll();
        Map<Long, List<Long>> collect = roleList.stream().collect(Collectors.groupingBy(EmployeeRole::getEmployeeId,
                Collectors.mapping(EmployeeRole::getRoleId,Collectors.toList())));
        for (Employee employee : employeeList) {
            employee.setRoles(collect.get(employee.getId()));
           /* employee.setRole(roleMapper.findByEmpId(employee.getId()));*/
        }
        return employeePageList;
    }
    @Override
    public void save(Employee employee) {


        String randomString = StrUtils.getComplexRandomString(32);
        Logininfo logininfo = new Logininfo();
        employee.setSalt(randomString);
        logininfo.setSalt(randomString);
        String s = MD5Utils.encrypByMd5(randomString + employee.getPassword());
        employee.setPassword(s);
        logininfo.setPassword(s);
        logininfo.setUsername(employee.getUsername());
        logininfo.setPhone(employee.getPhone());
        logininfo.setEmail(employee.getEmail());
        logininfoMapper.save(logininfo);
        employee.setLogininfoId(logininfo.getId());
        super.save(employee);
        List<Long> roles = employee.getRoles();
        employeeRoleMapper.saveId(employee.getId(),roles);
    }

    @Override
    public void update(Employee employee) {

        List<Long> roles = employee.getRoles();
        System.out.println(employee.getId()+"+++++++++");
        employeeRoleMapper.deleteById(employee.getId());
        employeeRoleMapper.saveId(employee.getId(),roles);


        String password = employee.getPassword();
        String randomString = StrUtils.getComplexRandomString(32);
        String s = MD5Utils.encrypByMd5(randomString + password);
        employee.setSalt(randomString);
        employee.setPassword(s);
        Logininfo logininfo = logininfoMapper.findById(employee.getLogininfoId());
        logininfo.setSalt(randomString);
        logininfo.setPassword(s);
        logininfoMapper.update(logininfo);
        super.update(employee);
    }

    @Override
    public void remove(Long id) {
        employeeRoleMapper.deleteById(id);
        Employee employee = employeeMapper.findById(id);
        logininfoMapper.remove(employee.getLogininfoId());
        super.remove(id);
    }

    @Override
    public Employee SeleteByLogId(Long id) {
        return employeeMapper.SeleteByLogId(id);
    }

    @Override
    public Employee findByEmpLoginId(Long id) {
        return employeeMapper.findByEmpLoginId(id);
    }
}
